package com.koron.configure.workflow.service.impl;


import cn.hutool.core.util.StrUtil;
import com.koron.common.core.business.workflow.bean.po.ConfigureWorkflowPlugin;
import com.koron.configure.workflow.bean.dto.ConfigureWorkflowPluginDTO;
import com.koron.configure.workflow.mapper.ConfigureWorkflowPluginMapper;
import com.koron.configure.workflow.service.ConfigureWorkflowPluginService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务配置管理-流程配置-插件(ConfigureWorkflowPlugin)表服务实现类
 *
 * @author jzp
 * @since 2022-01-11 11:36:45
 */
@Service
public class ConfigureWorkflowPluginServiceImpl implements ConfigureWorkflowPluginService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public ConfigureWorkflowPlugin getById(SessionFactory factory, String id) {
        ConfigureWorkflowPluginMapper mapper = factory.getMapper(ConfigureWorkflowPluginMapper.class);
        return mapper.selectById(id);
    }

    @Override
    public ConfigureWorkflowPluginDTO getDtoById(SessionFactory factory, String id) {
        ConfigureWorkflowPluginMapper mapper = factory.getMapper(ConfigureWorkflowPluginMapper.class);
        return mapper.selectDtoById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<ConfigureWorkflowPlugin> list(SessionFactory factory, ConfigureWorkflowPlugin configureWorkflowPlugin) {
        ConfigureWorkflowPluginMapper mapper = factory.getMapper(ConfigureWorkflowPluginMapper.class);
        return mapper.selectList(configureWorkflowPlugin);
    }

    @Override
    @TaskAnnotation("listDto")
    public List<ConfigureWorkflowPluginDTO> listDto(SessionFactory factory, ConfigureWorkflowPlugin configureWorkflowPlugin) {
        ConfigureWorkflowPluginMapper mapper = factory.getMapper(ConfigureWorkflowPluginMapper.class);
        return mapper.selectDtoList(configureWorkflowPlugin);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("listByWorkflowId")
    public List<ConfigureWorkflowPlugin> listByWorkflowId(SessionFactory factory, String workflowId) {
        if (StringUtils.isEmpty(workflowId)) {
            return new ArrayList<>();
        }
        ConfigureWorkflowPlugin configureWorkflowPlugin = new ConfigureWorkflowPlugin();
        configureWorkflowPlugin.setWorkflowId(workflowId);
        return this.list(factory, configureWorkflowPlugin);
    }

    @Override
    @TaskAnnotation("listDtoByWorkflowId")
    public List<ConfigureWorkflowPluginDTO> listDtoByWorkflowId(SessionFactory factory, String workflowId) {
        if (StringUtils.isEmpty(workflowId)) {
            return new ArrayList<>();
        }
        ConfigureWorkflowPlugin configureWorkflowPlugin = new ConfigureWorkflowPlugin();
        configureWorkflowPlugin.setWorkflowId(workflowId);
        return this.listDto(factory, configureWorkflowPlugin);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ConfigureWorkflowPlugin configureWorkflowPlugin) {
        ConfigureWorkflowPluginMapper mapper = factory.getMapper(ConfigureWorkflowPluginMapper.class);
        return mapper.insertOrUpdate(configureWorkflowPlugin) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<ConfigureWorkflowPlugin> configureWorkflowPlugin) {
        if (CollectionUtils.isEmpty(configureWorkflowPlugin)) {
            return false;
        }
        ConfigureWorkflowPluginMapper mapper = factory.getMapper(ConfigureWorkflowPluginMapper.class);
        return mapper.insertOrUpdateBatch(configureWorkflowPlugin) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ConfigureWorkflowPlugin configureWorkflowPlugin) {
        ConfigureWorkflowPluginMapper mapper = factory.getMapper(ConfigureWorkflowPluginMapper.class);
        return mapper.update(configureWorkflowPlugin) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        ConfigureWorkflowPluginMapper mapper = factory.getMapper(ConfigureWorkflowPluginMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByWorkflowId")
    public boolean removeByWorkflowId(SessionFactory factory, String workflowId){
        if (StrUtil.isEmpty(workflowId)) {
            return false;
        }
        ConfigureWorkflowPlugin configureWorkflowPlugin = new ConfigureWorkflowPlugin();
        configureWorkflowPlugin.setWorkflowId(workflowId);
        List<ConfigureWorkflowPlugin> list = list(factory, configureWorkflowPlugin);

        return deleteBatchIds(factory, list.stream().map(ConfigureWorkflowPlugin::getId).collect(Collectors.toList()));
    }
}
